package com.lagou.edu;


import com.alibaba.druid.pool.DruidDataSource;
import com.lagou.anno.Autowired;
import com.lagou.anno.Bean;
import com.lagou.anno.ComponentScan;
import com.lagou.anno.Configuration;
import com.lagou.anno.PropertySource;
import com.lagou.anno.Value;
import com.lagou.mapper.AnnoMapperConfig;
import com.lagou.mapper.MapperConfig;
import com.lagou.tx.SpringTractionManager;

import javax.sql.DataSource;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;

/**
 * @author 应癫
 */

// @Configuration 注解表明当前类是一个配置类
@Configuration
@ComponentScan("com.lagou.edu")
@PropertySource({"jdbc.properties"})
public class SpringConfig {

    @Value("${jdbc.driver}")
    private String driverClassName;
    @Value("${jdbc.url}")
    private String url;
    @Value("${jdbc.username}")
    private String username;
    @Value("${jdbc.password}")
    private String password;


    @Bean()
    public DataSource createDataSource(){
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setDriverClassName(driverClassName);
        druidDataSource.setUrl(url);
        druidDataSource.setUsername(username);
        druidDataSource.setPassword(password);
        return  druidDataSource;
    }

    @Bean
    public SpringTractionManager createSpringTractionManager(@Autowired DataSource dataSource){
        return new SpringTractionManager(dataSource);
    }

    @Bean
    public MapperConfig createConfig(@Autowired DataSource dataSource){
        return new AnnoMapperConfig("com.lagou.edu.dao",dataSource);
    }

    public static void main(String[] args) {
        Method[] methods = SpringConfig.class.getMethods();
        for (Method method : methods) {
            Parameter[] parameters = method.getParameters();
            for (Parameter parameter : parameters) {
                Autowired annotation = parameter.getAnnotation(Autowired.class);
                Class<?> type = parameter.getType();
                System.out.println(type);
            }

        }
    }
}
